/**
 * @name storage.js
 * @description token localStorage 处理
 * @author 'fwq'
 * @time 2021/10/17
 */
import { validatenull } from "./validate";

const keyName = "vTor" + "-";

const TokenKey = "mVite";

const getToken = () => {
  return getCookie(TokenKey);
};

const setToken = (token) => {
  return setCookie(TokenKey, token);
};

const removeToken = () => {
  return delCookie(TokenKey);
};

/**
 * 存储localStorage
 */
const setStore = (params = {}) => {
  let { name, content, type } = params;
  name = keyName + name;
  let obj = {
    dataType: typeof content,
    content: content,
    type: type,
    datetime: new Date().getTime()
  };
  if (type) window.sessionStorage.setItem(name, JSON.stringify(obj));
  else window.localStorage.setItem(name, JSON.stringify(obj));
};
/**
 * 获取localStorage
 */
const getStore = (params = {}) => {
  let { name, debug } = params;
  name = keyName + name;
  let obj = {},
    content;
  obj = window.sessionStorage.getItem(name);
  if (validatenull(obj)) obj = window.localStorage.getItem(name);
  if (validatenull(obj)) return;
  try {
    obj = JSON.parse(obj);
  } catch {
    return obj;
  }
  if (debug) {
    return obj;
  }
  if (obj.dataType == "string") {
    content = obj.content;
  } else if (obj.dataType == "number") {
    content = Number(obj.content);
  } else if (obj.dataType == "boolean") {
    content = eval(obj.content);
  } else if (obj.dataType == "object") {
    content = obj.content;
  }
  return content;
};
/**
 * 删除localStorage
 */
const removeStore = (params = {}) => {
  let { name, type } = params;
  name = keyName + name;
  if (type) {
    window.sessionStorage.removeItem(name);
  } else {
    window.localStorage.removeItem(name);
  }
};

/**
 * 获取全部localStorage
 */
const getAllStore = (params = {}) => {
  let list = [];
  let { type } = params;
  if (type) {
    for (let i = 0; i <= window.sessionStorage.length; i++) {
      list.push({
        name: window.sessionStorage.key(i),
        content: getStore({
          name: window.sessionStorage.key(i),
          type: "session"
        })
      });
    }
  } else {
    for (let i = 0; i <= window.localStorage.length; i++) {
      list.push({
        name: window.localStorage.key(i),
        content: getStore({
          name: window.localStorage.key(i)
        })
      });
    }
  }
  return list;
};

/**
 * 清空全部localStorage
 */
const clearStore = (params = {}) => {
  let { type } = params;
  if (type) {
    window.sessionStorage.clear();
  } else {
    window.localStorage.clear();
  }
};

/**
 * 获取cookie
 * @param name
 * @returns {string|null}
 */

const getCookie = (name) => {
  let reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
  let arr = document.cookie.match(reg);
  if (Array.isArray(arr) && arr.length) {
    return arr[2];
  } else {
    return null;
  }
};

/**
 * 设置cookie,增加到vue实例方便全局调用
 * @param c_name
 * @param value
 * @param expiredays
 */
const setCookie = (c_name, value, expireDays) => {
  let exDate = new Date();
  exDate.setDate(exDate.getDate() + expireDays);
  document.cookie =
    c_name + "=" + escape(value) + (expireDays == null ? "" : ";expires=" + exDate.toGMTString());
};

/**
 * 删除cookie
 * @param name
 */
const delCookie = (name) => {
  let exp = new Date();
  exp.setTime(exp.getTime() - 1);
  let cval = getCookie(name);
  if (cval != null) {
    document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString();
  }
};

export {
  getToken,
  setToken,
  removeToken,
  setStore,
  getStore,
  removeStore,
  getAllStore,
  clearStore,
  setCookie,
  getCookie,
  delCookie
};
